<?php

/**
 * This is the model class for table "packing_list_packages".
 */
class packing_list_packages extends ActiveRecord
{
	/**
	 * The followings are the available columns in table 'packing_list_packages':
	 * @var integer $id
	 * @var integer $packing_list_id
	 * @var string $marking
	 * @var integer $c_date
	 * @var integer $detail_id
	 */

	/**
	 * Returns the static model of the specified AR class.
	 * @return packing_list_packages the static model class
	 */
	public $cargo;
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'packing_list_packages';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('packing_list_id, marking, c_date', 'required', 'on'=>'not_packing'),
			array('packing_list_id, c_date, detail_id', 'numerical', 'integerOnly'=>true),
			array('marking', 'length', 'max'=>15),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, packing_list_id, marking, c_date, detail_id', 'safe', 'on'=>'search'),
		);
	}
	public function beforeValidate(){
		$this->c_date = time();
		return parent::beforeValidate();
	}
	public function afterFind(){
		if($this->detail == null) {
			$this->detail = new cargo_details;
			$this->detail->for_model = __CLASS__;
		}
		return parent::afterFind();
	}
	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'detail'=>array(self::HAS_ONE,'packing_list_packages_dimension','foreign_key','condition'=>'for_model="packing_list_packages"'),
			'packing_list'=>array(self::BELONGS_TO,'packing_list','packing_list_id'),
			'package_cargo'=>array(self::HAS_MANY,'packing_list_packages_cargo','package_id'),
			'cargo'=>array(self::MANY_MANY,'packing_list_packages','packing_list_packages_cargo(cargo_id,package_id)'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'marking' => 'Marking',
			'm3'=>'Dimension',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);

		$criteria->compare('packing_list_id',$this->packing_list_id);

		$criteria->compare('marking',$this->marking,true);

		$criteria->compare('c_date',$this->c_date);

		$criteria->compare('detail_id',$this->detail_id);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
}